package com.stonedt.spider.controller.complaint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.stonedt.spider.util.DateUtil;
import com.stonedt.spider.util.UtilConfig;

/**
 * 	投诉数据
 * @date  2020年2月6日 下午2:43:45
 */
@Controller
@RequestMapping("/complaintdata")
public class ComplaintDataController {
	
//	private final String es_url = "http://s1.stonedt.com:6303";
	private final String es_url = UtilConfig.getURL2().substring(0,UtilConfig.getURL2().length()-1);
	
	private final String complaintlist_api = UtilConfig.getURL2().substring(0,UtilConfig.getURL2().length()-1)+"/complain/complainlist";
	
	/***
	 * 投诉
	 * @param mav
	 * @return
	 */
	@RequestMapping("blackCatComplaint")
	public ModelAndView BlackCatComplaint(ModelAndView mv,HttpServletRequest request) {
		String classify=request.getParameter("classify");
		mv.setViewName("complaintData/ComplaintList");
		mv.addObject("classify", classify);
		if(classify.equals("h")) {
        	mv.addObject("webName", "黑猫投诉");
        }else if(classify.equals("j")) {
        	mv.addObject("webName", "聚投诉");
        }else if(classify.equals("x")) {
        	mv.addObject("webName", "消费宝");
        }
		mv.addObject("nowArray", DateUtil.getNowDate());
        request.setAttribute("LEFT", classify);
        
		return mv;
	}
	
	@RequestMapping("blackCat")
	public ModelAndView BlackCat(ModelAndView mav) {
		mav.setViewName("complaintData/article1");
		return mav;
	}
	
	
	@RequestMapping(value="/blackCat-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject RedBookData(
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page,@RequestParam("classify") String classify) {
		JSONObject json=new JSONObject();
		try {
			String response="";
			String sourceName="";
			String param="";
			if(classify.equals("h")) {
				sourceName="黑猫投诉";
			}else if(classify.equals("j")) {
				sourceName="聚投诉";
			}else if(classify.equals("x")) {
				sourceName="消费宝";
			}
			param="?times=&timee=&page="+page+"&size=10&sourceName="+sourceName;
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
				response = sendPost(complaintlist_api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.get("count").toString());
				page_count=Integer.valueOf(json.get("page_count").toString());
				size=Integer.valueOf(json.get("size").toString());
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	
	public String esArticle(Map<String, String> map, String api) {
		String size = map.get("size");
		String air_quality = map.get("air_quality");
		String times = map.get("times");
		String timee = map.get("timee");
		String page = map.get("page");
		String keyword = map.get("keyword");
		String url = es_url + api;
		String params = "size=" + size + "&air_quality=" + air_quality
				+ "&times=" + times + "&timee=" + timee + "&page=" + page + "&keyword=" + keyword;
		System.err.println(url + "?" + params);
		String sendPost = "";
		try {
			sendPost = sendPost(url, params);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sendPost;
	}

	public static String sendPost(String url, String params) throws IOException {
		PrintWriter out = null;
		BufferedReader in = null;
		URL realUrl = new URL(url);
	    URLConnection conn = realUrl.openConnection();  
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		out = new PrintWriter(conn.getOutputStream());
		out.print(params);
		out.flush();
		in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
		StringBuilder response = new StringBuilder();
		String line;
		while((line = in.readLine()) != null){
			response.append(line);
		}
		try {
			if (out != null) {
				out.close();
			}
			if (in != null) {
				in.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return response.toString();
	}
	
	
	/***
	 * 市长信箱
	 * @param mav
	 * @return
	 * Colin
	 */
	@RequestMapping("/mailbox")
	public ModelAndView mailbox(ModelAndView mav) {
		mav.addObject("nowArray", DateUtil.getNowDate());
		mav.setViewName("complaintData/mailbox");
		return mav;
	}
	
	
	@RequestMapping(value="/mailbox-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject mailboxData(
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			String response="";
			String param="";
			param="?times=&timee=&page="+page+"&size=10";
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
				String url=es_url+"/mayormailbox/mayormailboxlist";
				response = sendPost(url, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.get("count").toString());
				page_count=Integer.valueOf(json.get("page_count").toString());
				size=Integer.valueOf(json.get("size").toString());
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
}
